package com.sunnada.edu.system.pub.common;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;

public class Common {
	/***
	 * explain 获取登录用户ID
	 * 
	 * @author chs 2014-11-17 update chs 2014-11-17
	 * @param request
	 * @return String
	 */
	public static String getLoginUserId(HttpServletRequest request) {
		return (String) request.getSession().getAttribute(
				Constants.USER_ID_SESSION);
	}

	public static String getUserAreaCode(HttpServletRequest request) {
		return (String) request.getSession().getAttribute(
				Constants.USER_AREA_CODE);
	}


	/**
	 * explain 获取业务主键
	 * 
	 * @author zjc 2014-11-17 update zjc 2014-11-17
	 * @return String
	 */
	public synchronized static String getSystemSeqId() {
		PubDao dao = AppContext.getInstance().getPubDao();
		return dao.getSystemSeqId();
	}

	/**
	 * explain 获取角色表主键
	 * 
	 * @author zjc 2016-08-04 update zjc 2016-08-04
	 * @return String
	 */
	public synchronized static String getRoleSeqId() {
		PubDao dao = AppContext.getInstance().getPubDao();
		return dao.getRoleSeqId();
	}

	/**
	 * explain 获取s_sysuser表主键
	 * 
	 * @author zjc 2016-08-04 update zjc 2016-08-04
	 * @return String
	 */
	public synchronized static String getUserSeqId() {
		PubDao dao = AppContext.getInstance().getPubDao();
		return dao.getUserSeqId();
	}

	/** 给类的所有属性赋值，包括空属性空值 */
	public static HashMap<String, String> setMapByPropertys(Class clazz,
			HashMap<String, String> map) {
		if (clazz != null) {
			HashMap<String, String> params = new HashMap<String, String>();
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field f = fields[i];
				params.put(f.getName(),
						Utils.trimString(map.get(getName(f.getName()))));
			}
			return params;
		}
		return null;
	}

	public static String getName(String str) {
		char[] chars = str.toCharArray();
		StringBuffer sb = new StringBuffer();

		if (chars[0] <= 90 && chars[0] >= 65) {
			sb.append((char) (chars[0] + 32));
		} else {
			sb.append(chars[0]);
		}

		for (int i = 1; i < chars.length; i++) {
			if (chars[i] <= 90 && chars[i] >= 65) {
				sb.append("_");
				sb.append((char) (chars[i] + 32));
			} else {
				sb.append(chars[i]);
			}
		}
		return sb.toString().toUpperCase();
	}

	public static boolean hasRoles(HttpServletRequest request) {
		String userId = request.getSession().getAttribute(
				Constants.USER_ID_SESSION)
				+ "";
		if (Utils.isNotNull(userId)) {
			return true;
		}
		return false;
	}

	/**
	 * explain 字符补0
	 * 
	 * @author zjc 2015-9-24 update zjc 2015-9-24
	 * @return String
	 */
	public static String addZeroForNum(String str, int strLength) {
		int strLen = str.length();
		StringBuffer sb = null;
		if (strLen <= strLength) {
			while (strLen < strLength) {
				sb = new StringBuffer();
				sb.append("0").append(str);// 左(前)补0
				// sb.append(str).append("0");//右(后)补0
				str = sb.toString();
				strLen = str.length();
			}
		} else {
			str = str.substring(str.length() - strLength, str.length());
			;
		}

		return str;
	}

	/**
	 * explain 根据序列名获取下一个序列
	 * 
	 * @author chs 2014-11-17 update chs 2014-11-17
	 * @return String
	 */
	public synchronized static String getSeqId(String seqName) {
		if (Constants.DATEBASE_TYPE.equals("oracle")) {
			PubDao dao = AppContext.getInstance().getPubDao();
			return dao.getNextSeqId(seqName);
		} else {
			UUID uuid = UUID.randomUUID();
			return uuid.toString();
		}
	}
}
